#include "httpResponse.h"

using namespace std;
using namespace boost;
using namespace BOOST_SPIRIT_CLASSIC_NS;

bool	parse_response(char const* str, string& bd, list<string>& hd)
{
  rule<>	header_rule = *(alnum_p | blank_p | punct_p) >> ch_p('\r') >> ch_p('\n');
  rule<>	response_rule = *(header_rule)[push_back_a(hd)] >> (*(anychar_p))[assign_a(bd)];
  
  return parse(str, response_rule, end_p).full;
}

bool	parse_code(char const* hd, string& numReponse, string& message)
{
  rule<>	code_rule = "HTTP/1.1 " >> (*(digit_p))[assign_a(numReponse)] >> ' ' >> (*(alnum_p | blank_p))[assign(message)];

  return parse(hd, code_rule, end_p).full;
}

HttpResponse::HttpResponse(const std::string& response)
{
  _header = "";
  _body = "";
  list<string> hd;
  parse_response(response.c_str(), _body, hd);
  std::list<std::string>::iterator	it = hd.begin();
  while (it != hd.end())
    {
      _header.append(*it);
      it++;
    }
  parse_code((*(hd.begin())).c_str(), _numReponse, _message);
  cout << "=> " << _numReponse << " : " << _message << endl;
  cout << "---\n" << _header;
}

HttpResponse::~HttpResponse()
{
}

const std::string&  HttpResponse::getCode() const
{
    return this->_numReponse;
}

const std::string&  HttpResponse::getBody() const
{
    return this->_body;
}

const std::string&  HttpResponse::getHeader() const
{
    return this->_header;
}

const std::string&  HttpResponse::getMessage() const
{
    return this->_message;
}
